package com.example.leetcode.leetcode.interviewQuestion;

/**
 * @author zhuhaitao
 * @email zhuhaitao@imoran.net
 * @date 2020/12/24 2:34 下午
 * @description
 * 面试题 02.02. 返回倒数第 k 个节点
 * 1->2->3->4->5 k=2
 * result 4
 */
class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

public class Solution_02_02 {

    public static int kthToLast(ListNode head, int k) {
        ListNode left = head;
        int l=1,r=1;
        while (head != null){
            if (r- l >=k){
                l++;
                left = left.next;
            }
            r++;
            head = head.next;
        }
        return left.val;
    }
    public static void main(String[] args) {

    }
}
